<?php
/* SVN FILE: $Id: starter.php 255 2009-12-22 06:45:47Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Bank ACP - starters
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 255 $
* @lastrevision $Date: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/plugins/bank/adminAction/starter.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class plugin_admin_bank_starter extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->html = marketRegistry::loadTemplate('cp_skin_plugin_bank');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'bank_bank' ) );
		
		$redirect = array(
								'manage' => 'manageStarters',
								'add' => 'addStarter',
								'insert' => 'insertStarter',
								'edit' => 'editStarter',
								'update' => 'updateStarter',
								'delete' => 'deleteStarter',
							);
		
		if(!$redirect[$this->request['do']])
		{
			$this->request['do'] = 'manage';
		}
		
		$function = $redirect[$this->request['do']];
		$this->$function();
		
		/* Output */
		$this->registry->output->html_main .= $this->registry->output->global_template->global_frame_wrapper();
		$this->registry->output->sendOutput();			
	}
	
	private function manageStarters()
	{
		$this->registry->output->html = $this->html->manageStarters();
	}
	
	private function addStarter()
	{
		$starter = array(
							's_name' => "Bank Starter",
							's_description' => "",
							's_interest_time' => 3600,
							's_interest_rate' => 0,
							's_deposit_fee' => 0,
							's_withdraw_fee' => 0,
							'create_price' => 0,
							'delete_fee' => 0,
						);
		
		$this->registry->output->html = $this->html->starterForm($starter, 'insert', $this->lang->words['add_starter_title'], $this->lang->words['add_starter_button']);
	}
	
	private function insertStarter()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$this->DB->insert('market_bank_starter',array(
														's_name' => $this->request['s_name'],
														's_description' => IPSText::getTextClass('bbcode')->preDbParse(IPSText::getTextClass('editor')->processRawPost('s_description')),
														's_interest_time' => numbers::multiply(numbers::parseFloat($this->request['s_interest_time']),3600),
														's_interest_rate' => numbers::parseFloat($this->request['s_interest_rate']),
														's_deposit_fee' => numbers::parseFloat($this->request['s_deposit_fee']),
														's_withdraw_fee' => numbers::parseFloat($this->request['s_withdraw_fee']),
														'create_price' => numbers::parseInt($this->request['create_price']),
														'delete_fee' => numbers::parseInt($this->request['create_price']),
						)							);
		
		ipsRegistry::cache()->rebuildCache('ibm_bank_starter','ibmarket');
		
		$this->lang->words['bank_starter_added_log'] = sprintf($this->lang->words['bank_starter_added_log'],$this->request['s_name']);
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['bank_starter_added_log'] );
		
		$this->lang->words['bank_starter_added_redirect'] = sprintf($this->lang->words['bank_starter_added_redirect'],$this->request['s_name']);
		$this->registry->output->global_message = $this->lang->words['bank_starter_added_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=starter" );
	}
	
	private function editStarter()
	{
		if(!is_array($this->caches['ibm_bank_starter'][$this->request['starter']]))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=starter" );
		}
		
		$this->registry->output->html = $this->html->starterForm($this->caches['ibm_bank_starter'][$this->request['starter']], 'update', $this->lang->words['edit_starter_title'], $this->lang->words['edit_starter_button']);
	}
	
	private function updateStarter()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		if(!is_array($this->caches['ibm_bank_starter'][$this->request['starter']]))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=starter" );
		}
		
		$this->DB->update('market_bank_starter',array(
														's_name' => $this->request['s_name'],
														's_description' => IPSText::getTextClass('bbcode')->preDbParse(IPSText::getTextClass('editor')->processRawPost('s_description')),
														's_interest_time' => numbers::multiply(numbers::parseFloat($this->request['s_interest_time']),3600),
														's_interest_rate' => numbers::parseFloat($this->request['s_interest_rate']),
														's_deposit_fee' => numbers::parseFloat($this->request['s_deposit_fee']),
														's_withdraw_fee' => numbers::parseFloat($this->request['s_withdraw_fee']),
														'create_price' => numbers::parseInt($this->request['create_price']),
														'delete_fee' => numbers::parseInt($this->request['create_price']),
			),"starter_id=".$this->request['starter']);
		
		ipsRegistry::cache()->rebuildCache('ibm_bank_starter','ibmarket');
		
		$this->lang->words['bank_starter_edited_log'] = sprintf($this->lang->words['bank_starter_edited_log'],$this->request['s_name']);
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['bank_starter_edited_log'] );
		
		$this->lang->words['bank_starter_edited_redirect'] = sprintf($this->lang->words['bank_starter_edited_redirect'],$this->request['s_name']);
		$this->registry->output->global_message = $this->lang->words['bank_starter_edited_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=starter" );
	}
	
	private function deleteStarter()
	{
		if(!is_array($this->caches['ibm_bank_starter'][$this->request['starter']]))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=starter" );
		}
		
		$name = $this->caches['ibm_bank_starter'][$this->request['starter']]['s_name'];
		
		$this->DB->delete('market_bank_starter',"starter_id=".$this->request['starter']);
		
		ipsRegistry::cache()->rebuildCache('ibm_bank_starter','ibmarket');
		
		$this->lang->words['bank_starter_deleted_log'] = sprintf($this->lang->words['bank_starter_deleted_log'],$name);
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['bank_starter_deleted_log'] );
		
		$this->lang->words['bank_starter_deleted_redirect'] = sprintf($this->lang->words['bank_starter_deleted_redirect'],$name);
		$this->registry->output->global_message = $this->lang->words['bank_starter_deleted_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=starter" );
	}
}